import json
from flask import Blueprint
from flask import request
from flask import Response

from sqlalchemy import Column, String,Integer
from flask_sqlalchemy import SQLAlchemy 

from app.comm.db import db
from app.comm.db import jsonformater
from app.comm.pub import RestEntity
from app.comm.pub import class2dict

deptservice = Blueprint('dept', __name__,)


class Dept(db.Model):
    __tablename__ = 'sys_dept'
    user_id = Column(Integer, primary_key=True)
    user_name = Column(String(80), unique=True, nullable=False)
    email = Column(String(120), unique=True, nullable=False)
    def as_dict(self):
        return {c.name: str(getattr(self, c.name)) for c in self.__table__.columns}
    def __repr__(self):
        return '<User %r>' % self.username


 
@deptservice.route("/rest/system/dept/treeData",methods=['POST','GET'])
def deptTreeData():  
    #dict1 = json.loads(a)
    tables=db.session.execute("select dept_id id,parent_id pId,dept_name name,dept_name title from sys_dept").fetchall()
    datas= [dict(r) for r in tables] 
    result=RestEntity(0,0,datas,"登陆成功！")
    return json.dumps(class2dict(result), default=jsonformater)
 
    #return json.dumps(dict1)     
@deptservice.route("/rest/system/dept/list",methods=['POST','GET'])
def deptList():  
    #dict1 = json.loads(a)
    tables=db.session.execute("select dept_id deptId,parent_id parentId,dept_name deptName,leader,order_num orderNum,status,create_time createTime from sys_dept").fetchall()
    datas= [dict(r) for r in tables] 
    result=RestEntity(0,0,datas,"登陆成功！")
    return json.dumps(datas, default=jsonformater)
 
    #return json.dumps(dict1)         